/***************************************************************************\
 * Copyright (C) by Keio University
 * test.cpp created in 10 2012.
 * Mail : fdesorbi@hvrl.ics.keio.ac.jp
 *
 * test.cpp is part of the HVRL Engine Project.
 *
 * The HVRL Engine Project is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * The HVRL Engine Project is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 \***************************************************************************/

#include "hvrl/tools/RigidTransformationEstimation.hpp"
#include <limits.h>
#include <gtest/gtest.h>
#include <time.h>

TEST(leastSquaresEstimation, SingularValuesSorted){

	cv::Mat srcA(3,5,CV_32F);
	cv::Mat srcB(3,5,CV_32F);

	srand ( time(NULL) );


	srand ( time(NULL) );

	for(unsigned int i = 0; i < 5; ++i){
		float x = (float(rand())/float(RAND_MAX)*2.0f - 1.0f) * 10.0f;
		float y = (float(rand())/float(RAND_MAX)*2.0f - 1.0f) * 10.0f;
		float z = (float(rand())/float(RAND_MAX)*2.0f - 1.0f) * 10.0f;

		srcA.at<float>(0,i) = x;
		srcA.at<float>(1,i) = y;
		srcA.at<float>(2,i) = z;

		srcB.at<float>(0,i) = x*2.0f + 3.0f;
		srcB.at<float>(1,i) = y*2.0f + 4.0f;
		srcB.at<float>(2,i) = z*2.0f + 5.0f;
	}

	cv::Mat R(3,3,CV_32F);
	cv::Vec3f t;

	hvrl::RigidTransformationEstimation::leastSquaresEstimation(srcA, srcB, R, t);


	for(unsigned int i = 0; i < 5; ++i){

		cv::Vec3f p;
		p[0] = srcA.at<float>(0,i);
		p[1] = srcA.at<float>(1,i);
		p[2] = srcA.at<float>(2,i);

		/*cv::Vec3f res = R*p+t;

	p[0] = srcB.at<float>(0,i);
		p[1] = srcB.at<float>(1,i);
		p[2] = srcB.at<float>(2,i);

		ASSERT_NEAR(res[0],p[0],0.0001f);
		ASSERT_NEAR(res[1],p[1],0.0001f);
		ASSERT_NEAR(res[2],p[2],0.0001f);*/

	}



}
